package com.app.piemoment.util;

import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Build;
import android.os.Environment;
import android.view.Window;
import android.view.WindowManager;

import com.app.piemoment.entity.VersionInfo;
import com.app.piemoment.log.PMLog;

public class Utils {

	private static final String TAG = "Utils";

	public static final int byteToShort(byte[] bytes) {
		return (bytes[0] << 8) + (bytes[1] & 0xFF);
	}

	public static boolean isSDCardEnable() {
		String SDState = Environment.getExternalStorageState();
		if (SDState != null && SDState.equals(android.os.Environment.MEDIA_MOUNTED)) {
			return true;
		}
		return false;
	}
	/**
	 * 将dip转换为pix
	 * 
	 * @param context
	 * @param dip
	 * @return
	 */
	public static int dipToPixels(Context context, float dip) {
		return (int) (context.getResources().getDisplayMetrics().density * dip);
	}


	public static VersionInfo.UpdateType getAppVersionUpdateType(Context context, String newVersion) {
		VersionInfo.UpdateType type = VersionInfo.UpdateType.NO_UPDATE;
		String localVersion = getLocalAppVersion(context);
		try {
			int[] localVersionArray = getVersionNum(localVersion);
			int[] newVersionArray = getVersionNum(newVersion);
			if (newVersionArray[0] > localVersionArray[0]) {
				return VersionInfo.UpdateType.UPDATE_AND_PROMPT;
			} else if (newVersionArray[0] == localVersionArray[0]) {
				if (newVersionArray[1] > localVersionArray[1]) {
					return VersionInfo.UpdateType.UPDATE_AND_PROMPT;
				} else if (newVersionArray[1] == localVersionArray[1]) {
					if (newVersionArray[2] > localVersionArray[2]) {
						return VersionInfo.UpdateType.UPDATE_AND_PROMPT;
					} else if (newVersionArray[2] == localVersionArray[2]) {
						if (newVersionArray[3] > localVersionArray[3]) {
							return VersionInfo.UpdateType.UPDATE_NO_PROMPT;
						}
					}
				}
			}
		} catch (Exception e) {
			PMLog.e(TAG, e);
		}
		return type;
	};

	public static int[] getVersionNum(String version) {
		int[] versionIntArray = new int[4];
		String[] versionStringArray = version.split("[.]");
		int versionStringLenth = versionStringArray.length;
		for (int i = 0; i < 4; i++) {
			if (i < versionStringLenth) {
				try {
					versionIntArray[i] = Integer.parseInt(versionStringArray[i]);
				} catch (Exception e) {
					PMLog.e(TAG, e);
					versionIntArray[i] = 0;
				}
			} else {
				versionIntArray[i] = 0;
			}
		}
		return versionIntArray;
	}

	/**
	 * 获取本地app版本号
	 * 
	 * @param context
	 * @return
	 */
	public static String getLocalAppVersion(Context context) {
		PackageManager pm = context.getPackageManager();
		String version = null;
		try {
			version = pm.getPackageInfo("com.gozap.chouti", 0).versionName;
		} catch (NameNotFoundException e) {
			PMLog.e(TAG, e);
		}
		return version;
	}

	/**
	 * 获取屏幕宽度(像素)
	 * 
	 * @param context
	 * @return
	 */
	public static int getScreenWidth(Context context) {
		WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		return windowManager.getDefaultDisplay().getWidth();
	}

	/**
	 * 获取状态栏高度
	 * 
	 * @param window
	 * @return
	 */
	public static int getStatusBarHeight(Window window) {
		Rect outRect = new Rect();
		window.getDecorView().getWindowVisibleDisplayFrame(outRect);
		return outRect.top;
	}

	/**
	 * 获取屏幕高度(像素)
	 * 
	 * @param context
	 * @return
	 */
	public static int getScreenHeight(Context context) {
		WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		return windowManager.getDefaultDisplay().getHeight();
	}

	/**
	 * 根据不同设备的分辨率获取适合的图片尺寸
	 * 
	 * @param url
	 * @return
	 */
	public static String getFitScreenImageUrl(String url, int size) {

		if(size<=0){
			return url;
		}
		
		String returnUrl = "";
		int i = url.lastIndexOf(".");
		int length = url.length();
		String urlName = "";
		String suffix = "";
		if (i < length && i > 0) {
			urlName = url.substring(0, i);
			suffix = url.substring(i, length);
		} else {
			urlName = url;
		}
		int imageW = size;
		/**
		 * 锁定比例,按照固定尺寸取小值缩小(只允许缩小) 协议类型 < width >x< height>) 例如：600x200)
		 * 首先该协议转换后的图片不能是放大的，缩小的时候按照比例，取小值进行缩小
		 */
		returnUrl = urlName + "=C" + imageW + "x" + imageW + suffix + "?quality=70";
		return returnUrl;
	}

	/**
	 * 获取设备版本
	 */
	public static String getPlatform() {
		String releaseversion = Build.VERSION.RELEASE;
		String version = "AR";
		if (releaseversion.contains("1.5")) {
			version = "AR_15";
		} else if (releaseversion.contains("1.6")) {
			version = "AR_16";
		} else if (releaseversion.contains("2.0")) {
			version = "AR_20";
		} else if (releaseversion.contains("2.1")) {
			version = "AR_21";
		} else if (releaseversion.contains("2.2")) {
			version = "AR_22";
		} else if (releaseversion.contains("2.3")) {
			version = "AR_23";
		} else if (releaseversion.contains("3.0")) {
			version = "AR_30";
		} else if (releaseversion.contains("3.1")) {
			version = "AR_31";
		} else if (releaseversion.contains("3.2")) {
			version = "AR_32";
		} else if (releaseversion.contains("4.0")) {
			version = "AR_40";
		}
		return version;
	}

	
	public static Bitmap toRoundBitmap(Bitmap bitmap) {
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		float roundPx;
		float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
		if (width <= height) {
			roundPx = width / 2;
			top = 0;
			bottom = width;
			left = 0;
			right = width;
			height = width;
			dst_left = 0;
			dst_top = 0;
			dst_right = width;
			dst_bottom = width;
		} else {
			roundPx = height / 2;
			float clip = (width - height) / 2;
			left = clip;
			right = width - clip;
			top = 0;
			bottom = height;
			width = height;
			dst_left = 0;
			dst_top = 0;
			dst_right = height;
			dst_bottom = height;
		}

		Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect src = new Rect((int) left, (int) top, (int) right,
				(int) bottom);
		final Rect dst = new Rect((int) dst_left, (int) dst_top,
				(int) dst_right, (int) dst_bottom);
		final RectF rectF = new RectF(dst);

		paint.setAntiAlias(true);

		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, src, dst, paint);
		return output;
	}

}
